package com.gxa.admin.controller;


import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;

import com.gxa.common.vo.ResultVo;
import com.gxa.modules.pojo.Checkreport_Document;
import com.gxa.modules.pojo.LoanSearch;

import com.gxa.modules.pojo.Loanafter_Checkreport;
import com.gxa.modules.service.Loanafter_CheckReportService;

import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "数据test",tags = "贷后管理——贷后检查——张剑宇")
@RestController

//1.新增贷后检查报告
@RequestMapping("/loanafter")
@RequiresPermissions("/home/afterloan/inspect")
public class Loanafter_CheckReportController {

    @Autowired(required = false)
    private Loanafter_CheckReportService loanafter_CheckReportService;

    @PostMapping("/loanafter_checkreport/add")
    @ApiOperation(value = "增加贷后检查报告",notes = "贷后检查报告实体",httpMethod = "POST", response = ResultVo.class)

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏请求"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=405,message="Method请求错误"),
            @ApiResponse(code=500,message="后台服务错误")
    })

    public ResultVo add(@RequestBody @ApiParam(name = "loanafter_checkreport",value = "贷后检查报告实体",required = true) Loanafter_Checkreport loanafter_checkreport){

        ResultVo r=loanafter_CheckReportService.addReport(loanafter_checkreport);
        return r;
    }
    //2.查询贷后检查报告
        @PostMapping("/loanafter_checkreport/query")
        @ApiOperation(value = "增加贷后检查报告",notes = "贷后检查报告实体",httpMethod = "POST", response = ResultVo.class)

        @ApiResponses({
                @ApiResponse(code=200,message="请求成功"),
                @ApiResponse(code=400,message="坏请求"),
                @ApiResponse(code=404,message="找不到页面"),
                @ApiResponse(code=405,message="Method请求错误"),
                @ApiResponse(code=500,message="后台服务错误")
        })

        public JsonResult query(@RequestBody @ApiParam(name = "pageRecv",value = "贷后检查报告实体",required = true) PageRecv<LoanSearch> pageRecv){

            JsonResult r=loanafter_CheckReportService.queryReport(pageRecv);
            return r;
        }

    //10.附件上传

    @PostMapping(value = "/addDocument")
    @ApiOperation(value = "附件上传接口",notes = "上传文件",httpMethod = "POST", response = ResultVo.class)


    public ResultVo addDocument(com.gxa.modules.pojo.File uploadfile, HttpServletRequest request) throws IOException {
        ResultVo r=new ResultVo();
        if(uploadfile.getFileList().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中文件");

        }else{//正常上传
            String uploadPath = request.getServletContext().getRealPath("") + java.io.File.separator + "upload" + java.io.File.separator;
            java.io.File file = new java.io.File(uploadPath);
            Map<String ,List<com.gxa.modules.pojo.File>> result= new HashMap<>();
            List<com.gxa.modules.pojo.File> resultlist= new ArrayList<>();
            if(!file.exists()){
                file.mkdirs();
            }
            List<MultipartFile> list = uploadfile.getFileList();
            for (MultipartFile file1: list
                    ) {
                String fileName = file1.getOriginalFilename();
                java.io.File newFile = new java.io.File(uploadPath + fileName);
                System.out.println("newFile=" + newFile);
                System.out.println(file1.getContentType());
                System.out.println("文件名"+fileName);

                String size=("文件大小"+String.format("%.2f",(double)file1.getSize()/1024/1024));
                //上传
                FileCopyUtils.copy(file1.getBytes(),newFile);

                com.gxa.modules.pojo.File f=new com.gxa.modules.pojo.File();
                f.setFileUrl(java.io.File.separator + "upload" + java.io.File.separator+fileName);
                f.setFileName(fileName);
                f.setFilesize(size);
                resultlist.add(f);

            }
            result.put("fileList",resultlist);
            r.setData(result);
            r.setCode(200);
            r.setMsg("上传成功");

        }


        return r;
    }

    //7.附件入库
    @PostMapping(value = "/saveDocument")
    @ApiOperation(value = "附件入库",notes = "附件入库",httpMethod = "POST", response = ResultVo.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResultVo saveDocument(@RequestBody @ApiParam(name = "document",value = "附件实体集合",required = true) List<Checkreport_Document> documentList){
        ResultVo r=new ResultVo();
        for(Checkreport_Document document:documentList){
            System.out.println(document);
            r = loanafter_CheckReportService.addDocument(document);
        }

        return r;
    }
}
